//
//  LikeShoppingCollectionViewCell.m
//  NineGroupProject
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LikeShoppingCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "LikeImageCollectionViewCell.h"
#import "Masonry.h"

@implementation LikeShoppingCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self creacteSubCell];
    }
    return self;
}


-(void)creacteSubCell{
    
    
    self.titleView = [[UIImageView alloc]init];
    self.titleView.layer.masksToBounds = YES;
    self.titleView.layer.cornerRadius = 30 * WID;
    self.titleView.userInteractionEnabled = YES;
    self.titleView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.titleView];
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10 * WID);
        make.top.mas_equalTo(5 * HEI);
        make.size.mas_equalTo(60 * WID);
    }];
    
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleView.mas_right).offset(10 * WID);
        make.top.mas_equalTo(10 * HEI);
        make.width.mas_equalTo(245 * WID);
        make.height.mas_equalTo(20 * HEI);
    }];
    
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.textColor = [UIColor grayColor];
    self.timeLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_top);
        make.right.mas_equalTo(-2 * WID);
        make.width.mas_equalTo(60 * WID);
        make.height.equalTo(self.titleLabel.mas_height);
    }];
    
    self.likeLabel = [[UILabel alloc]init];
    self.likeLabel.textColor = [UIColor grayColor];
    self.likeLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:self.likeLabel];
    [self.likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10 * WID);
        make.width.equalTo(self.titleLabel.mas_width);
        make.height.equalTo(self.titleLabel.mas_height);
    }];
    
    self.allArray = [NSMutableArray array];
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    // 设置滚动方向
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.flowLayout.itemSize = CGSizeMake(125 * WID, 160 * HEI);
    self.flowLayout.minimumLineSpacing = 8 * HEI;
    // 设置最小列间距
    self.flowLayout.minimumInteritemSpacing = 10 * WID;
    // 设置item与四周边界的距离  上左下右
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10 * HEI, 10 * WID, 10 * HEI, 10 * WID);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(80 * WID, 80 * HEI, WIDTH - 90 * WID, 180 * HEI) collectionViewLayout:self.flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerClass:[LikeImageCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
}

-(void)setModel:(LikeModel *)model{
    _model = model;
    self.likeLabel.text = [NSString stringWithFormat:@"喜欢了%@件商品", self.model.feedsSize];
    self.titleLabel.text = self.model.nickName;
    self.timeLabel.text = self.model.time;
    NSURL *url = [NSURL URLWithString:self.model.avatarPath];
    [self.titleView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholderImage.png"]];
    
    self.num = [self.model.feedsSize integerValue];
    
    // 将数组中的的数据清空然后刷新collectionView
    [self.allArray removeAllObjects];
    [self.collectionView reloadData];
    // 重新给数组添加请求到的数据
    self.allArray = [NSMutableArray arrayWithArray:model.feeds];
    [self.collectionView reloadData];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.num;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *filePath = @"cell";
    LikeImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:filePath forIndexPath:indexPath];
    
    NSMutableDictionary *dic = [self.allArray objectAtIndex:indexPath.row];
    cell.dic=dic;
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *productId = [[self.allArray objectAtIndex:indexPath.item] objectForKey:@"productId"];
    
    
    [self.likeShoppingCollectionViewDelegate byDetailViewController:productId];
    
    
}


@end
